package com.czy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import okhttp3.*;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class 修改随手拍时间 {

    private static final String TOKEN = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJzeXNfdXNlcjoxIiwicm5TdHIiOiIxSGZEVDE1Q0I3Nzh2YTRvcFZYWmZVUURJNmhJbEpkSyIsInVzZXJJZCI6MX0.00Qyccz3qhBEyW0PNbnNc3CbAgvgVt_HOybJlJFT1FU";
    private static final String URL = "https://sjzf.tzdxyf.com:9004/prod-api/sjzf/casualPhotograph/update"; // 🔧 改成你的接口地址

    private static final OkHttpClient client = new OkHttpClient();
    private static final ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\Administrator\\OneDrive\\Desktop\\新建 文本文档 (2).txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String s = br.readLine();
        List<JSONObject> jsonObjects = JSONArray.parseArray(s, JSONObject.class);
        Map<String, JSONObject> map = jsonObjects.stream().collect(Collectors.toMap(item -> item.getString("id"), item -> item));

        List<Map<String, String>> dataList = new ArrayList<>(List.of(
                new HashMap<>(Map.of("id", "1976532289845231618", "submitTime", "2025-09-05", "rectificationTime", "2025-09-08")),
                new HashMap<>(Map.of("id", "1983732230975946754", "submitTime", "2025-09-09", "rectificationTime", "2025-09-12")),
                new HashMap<>(Map.of("id", "1983731935080382465", "submitTime", "2025-09-13", "rectificationTime", "2025-09-17")),
                new HashMap<>(Map.of("id", "1983731665994809345", "submitTime", "2025-09-16", "rectificationTime", "2025-09-18")),
                new HashMap<>(Map.of("id", "1976531532223905794", "submitTime", "2025-09-19", "rectificationTime", "2025-09-23")),
                new HashMap<>(Map.of("id", "1983490659412115457", "submitTime", "2025-09-21", "rectificationTime", "2025-09-24")),
                new HashMap<>(Map.of("id", "1983489949702324226", "submitTime", "2025-09-23", "rectificationTime", "2025-09-26")),
                new HashMap<>(Map.of("id", "1983488652198576129", "submitTime", "2025-09-27", "rectificationTime", "2025-09-28")),
                new HashMap<>(Map.of("id", "1983733782834872322", "submitTime", "2025-09-28", "rectificationTime", "2025-09-30")),
                new HashMap<>(Map.of("id", "1983471277881458689", "submitTime", "2025-09-29", "rectificationTime", "2025-10-09")),
                new HashMap<>(Map.of("id", "1983515120827924481", "submitTime", "2025-10-05", "rectificationTime", "2025-10-08")),
                new HashMap<>(Map.of("id", "1983470465335721986", "submitTime", "2025-10-06", "rectificationTime", "2025-10-10")),
                new HashMap<>(Map.of("id", "1983729528665571330", "submitTime", "2025-10-10", "rectificationTime", "2025-10-13")),
                new HashMap<>(Map.of("id", "1983730325067431937", "submitTime", "2025-10-13", "rectificationTime", "2025-10-16")),
                new HashMap<>(Map.of("id", "1983715817355771905", "submitTime", "2025-10-14", "rectificationTime", "2025-10-18")),
                new HashMap<>(Map.of("id", "1976530219142512642", "submitTime", "2025-10-16", "rectificationTime", "2025-10-20")),
                new HashMap<>(Map.of("id", "1983729528665571330", "submitTime", "2025-10-18", "rectificationTime", "2025-10-22")),
                new HashMap<>(Map.of("id", "1983716837234343938", "submitTime", "2025-10-19", "rectificationTime", "2025-10-21")),
                new HashMap<>(Map.of("id", "1983727582302994433", "submitTime", "2025-10-22", "rectificationTime", "2025-10-27")),
                new HashMap<>(Map.of("id", "1976530871583277058", "submitTime", "2025-10-25", "rectificationTime", "2025-10-28")),
                new HashMap<>(Map.of("id", "1983721224170143745", "submitTime", "2025-10-25", "rectificationTime", "2025-10-28")),
                new HashMap<>(Map.of("id", "1983733201999265794", "submitTime", "2025-10-26", "rectificationTime", "2025-10-29")),
                new HashMap<>(Map.of("id", "1983726569961586690", "submitTime", "2025-10-27", "rectificationTime", "2025-10-29")),
                new HashMap<>(Map.of("id", "1976531981698105345", "submitTime", "2025-10-28", "rectificationTime", "2025-10-30")),
                new HashMap<>(Map.of("id", "1983517406237065218", "submitTime", "2025-10-29", "rectificationTime", "2025-10-30"))
        ));
        for (Map<String, String> item : dataList) {
            // 根据 id 找到 jsonObjects 中对应的原始记录
            JSONObject jsonObject = map.get(item.get("id"));

            if (jsonObject != null) {
                // submitTime
                String originalSubmitTime = jsonObject.getString("submitTime"); // "2025-08-01 14:23:45"
                if(originalSubmitTime != null) {
                    String submitDate = item.get("submitTime"); // "2025-09-05"
                    String submitTimeWithHMS = submitDate + originalSubmitTime.substring(10); // 拼成 "2025-09-05 14:23:45"
                    item.put("submitTime", submitTimeWithHMS);
                }

                // rectificationTime
                String originalRectTime = jsonObject.getString("rectificationTime");
                if(originalRectTime != null) {
                    String rectDate = item.get("rectificationTime");
                    String rectTimeWithHMS = rectDate + originalRectTime.substring(10);
                    item.put("rectificationTime", rectTimeWithHMS);
                }

                sendPost(item);
            } else {
                System.out.println("找不到ID对应的原始记录: " + item.get("id"));
            }
        }
    }

    private static void sendPost(Map<String, String> item) throws IOException {
        String json = mapper.writeValueAsString(item);

        Request request = new Request.Builder()
                .url(URL)
                .addHeader("Authorization", TOKEN)
                .addHeader("Content-Type", "application/json")
                .post(RequestBody.create(json, MediaType.parse("application/json")))
                .build();

        try (Response response = client.newCall(request).execute()) {
            System.out.println("ID: " + item.get("id") + " → " + response.code() + " " + response.message());
            if (response.body() != null) {
                System.out.println(response.body().string());
            }
        }
    }
}
